<?php

namespace mz\ars;

use mz\yiiext\MyPagination;
use Yii;
use yii\data\Pagination;

/**
 * This is the model class for table "{{%article_base}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $thumb
 * @property string $keywords
 * @property string $desc
 * @property string $content
 * @property integer $category_id
 * @property integer $created_uid
 * @property integer $last_change
 * @property integer $status
 * @property integer $isdel
 */
class Article extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%article}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['content'], 'required'],
            [['content'], 'string'],
            [['category_id', 'created_uid', 'last_change', 'status', 'isdel'], 'integer'],
            [['title', 'thumb', 'desc'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'thumb' => 'Thumb',
            'desc' => 'Desc',
            'content' => 'Content',
            'category_id' => 'Category ID',
            'created_uid' => 'Created Uid',
            'last_change' => 'Last Change',
            'status' => 'Status',
            'isdel' => 'Isdel',
        ];
    }

    /**
     * 获取所有文章
     * @param array $map
     * @return array
     */
    public static function getAllArticle($map = [])
    {
        $query = static::find();
        if(isset($map['title'])){
            $query->andFilterWhere(['like','title',$map['title']]);
        }
        $pageSize = isset($map['pageSize']) ? $map['pageSize'] : 10;
        $countQuery = clone $query;
        $pages = new MyPagination([
            'pageSize' => $pageSize,
            'totalCount' => $countQuery->count(),
            'defaultPageSize' => $pageSize
        ]);

        $query->orderBy("last_change desc");
        $items = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->asArray()
            ->all();

        return compact('items','pages');
    }

    /**
     * 获取文章详情
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getFindOne($id)
    {
        $query = static::find()->where(['id' => $id])->asArray()->one();
        return $query;
    }

}
